<div class="layui-fluid" id="component-tabs">
    <div class="layui-row">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body">
                    <div class="layui-tab layui-tab-brief" lay-filter="component-tabs-brief">
                        <ul class="layui-tab-title">
                            <li class="layui-this">基本信息</li>
                            <li>发货记录</li>
                            <li>操作日志</li>
                        </ul>
                        <div class="layui-tab-content">
                            <div class="layui-tab-item layui-show" id="detail"></div>
                            <script id="detail_tpl" type="text/html">
                                <form class="layui-form" method="post">
                                    <table style="width: 100%">
                                        <tr>
                                            <td valign="top" style="width: 33%;">
                                                <table class="layui-table" style="width: 99%;">
                                                    <tbody>
                                                    <tr>
                                                        <td width="100">订单号：</td>
                                                        <td>{{ d.order.order_no }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>商品总金额：</td>
                                                        <td>￥{{ d.order.sell_price_total }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>邮费金额：</td>
                                                        <td>￥{{ d.order.delivery_price_real }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>优惠金额：</td>
                                                        <td>￥{{ d.order.promotion_price }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>改价金额：</td>
                                                        <td>￥{{ d.order.discount_price }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>支付金额：</td>
                                                        <td>￥{{ d.order.subtotal }}</td>
                                                    </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                            <td valign="top" style="width: 34%;">
                                                <table class="layui-table" style="width: 99%;">
                                                    <tbody>
                                                    <tr>
                                                        <td width="100">收货人姓名：</td>
                                                        <td>{{ d.order.full_name }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>收货人电话：</td>
                                                        <td>{{ d.order.tel }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>收货地址：</td>
                                                        <td>{{ d.order.prov }}{{ d.order.city }}{{ d.order.area }}{{ d.order.address }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>送货时间：</td>
                                                        <td>{{ d.order.delivery_time }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>配送方式：</td>
                                                        <td>{{ d.order.delivery_type }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>备注：</td>
                                                        <td>{{ d.order.note }}</td>
                                                    </tr>
                                                    </tbody>
                                                </table>
                                            </td>
                                            <td valign="top" style="width: 33%;">
                                                <table class="layui-table" style="width: 99%;">
                                                    <tbody>
                                                    <tr>
                                                        <td width="100">状态：</td>
                                                        <td><span class="layui-badge">{{ d.order.status_label }}</span></td
                                                    </tr>
                                                    <tr>
                                                        <td>下单用户：</td>
                                                        <td>{{ d.order.username }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>支付方式：</td>
                                                        <td>{{ d.order.payment_name }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>第三方单号：</td>
                                                        <td>{{ d.order.payment_no }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>所属店铺：</td>
                                                        <td>{{ d.seller.title }}</td>
                                                    </tr>
                                                    {{# if(d.invoice){ }}
                                                    <tr>
                                                        <td>发票抬头：</td>
                                                        <td>{{ d.invoice.type_text }}/{{ d.invoice.title }}</td>
                                                    </tr>
                                                    <tr>
                                                        <td>发票信息：</td>
                                                        <td>{{ d.invoice.tax_no }}</td>
                                                    </tr>
                                                    {{# } }}
                                                    </tbody>
                                                </table>
                                            </td>
                                        </tr>
                                    </table>
                                    <table class="layui-table">
                                        <thead>
                                        <tr>
                                            <th style="min-width: 300px;">商品名称</th>
                                            <th width="300">规格</th>
                                            <th width="100">商品价格</th>
                                            <th width="100">商品数量</th>
                                            <th width="100" style="text-align: center;">售后状态</th>
                                            <th width="100" style="text-align: center;">发货状态</th>
                                            <th width="80" style="text-align: center;">发货</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        {{# layui.each(d.goods, function(index, item){ }}
                                        <tr>
                                            <td>{{ item.goods_title }}</td>
                                            <td>{{ item.spec_value }}</td>
                                            <td>{{ item.sell_price }}</td>
                                            <td>{{ item.buy_qty }}</td>
                                            <td style="text-align: center;">{{ item.refund }}</td>
                                            <td style="text-align: center;">{{ item.delivery_text }}</td>
                                            <td style="text-align: center;">
                                                {{# if(item.delivery == 0){ }}
                                                <input type="checkbox" name="order_goods_id[]" value="{{ item.id }}" lay-skin="primary" title="" checked>
                                                {{# } }}
                                            </td>
                                        </tr>
                                        {{# }); }}
                                        </tbody>
                                    </table>
                                    {{# if(d.order.is_pay){ }}
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <button class="layui-btn" lay-submit="" lay-filter="order-order-detail-cancel">取消订单</button>
                                        </div>
                                    </div>
                                    {{# } }}
                                    {{# if(d.order.is_delivery){ }}
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">物流公司</label>
                                        <div class="layui-input-inline">
                                            <select name="company_id" lay-verify="required" lay-reqText="请选择物流公司">
                                                <option value=""></option>
                                                {{# layui.each(d.express_company, function(index, item){ }}
                                                <option value="{{ index }}">{{ item }}</option>
                                                {{# }); }}
                                            </select>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">快递单号</label>
                                        <div class="layui-input-inline">
                                            <input type="text" name="code" lay-verify="required" lay-reqText="快递单号不能为空" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label class="layui-form-label">备注</label>
                                        <div class="layui-input-block" style="width: 400px;">
                                            <textarea name="note" class="layui-textarea"></textarea>
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <div class="layui-input-block">
                                            <button class="layui-btn" lay-submit="" lay-filter="order-order-detail-delivery">发货</button>
                                        </div>
                                    </div>
                                    {{# } }}
                                    <input type="hidden" name="id" value="{{ d.order.id }}"/>
                                </form>
                            </script>
                            <div class="layui-tab-item" id="delivery"></div>
                            <script id="delivery_tpl" type="text/html">
                                <table class="layui-table">
                                    <thead>
                                    <tr>
                                        <th width="170" style="text-align: center;">时间</th>
                                        <th width="120">物流公司</th>
                                        <th width="80">物流单号</th>
                                        <th width="80">物流状态</th>
                                        <th>备注</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {{# layui.each(d, function(index, item){ }}
                                    <tr>
                                        <td style="text-align: center;">{{ item.created_at }}</td>
                                        <td>{{ item.company_name }}</td>
                                        <td>{{ item.code }}</td>
                                        <td>
                                            <button type="button" class="layui-btn layui-btn-xs" lay-filter="order-order-detail-delivery_traces" data-company_code="{{ item.company_code }}" data-code="{{ item.code }}">查看状态</button>
                                        </td>
                                        <td>{{ item.note }}</td>
                                    </tr>
                                    {{# }); }}
                                    </tbody>
                                </table>
                            </script>
                            <div class="layui-tab-item" id="log"></div>
                            <script id="log_tpl" type="text/html">
                                <table class="layui-table">
                                    <thead>
                                    <tr>
                                        <th width="170" style="text-align: center;">时间</th>
                                        <th width="120">操作用户</th>
                                        <th width="80">操作动作</th>
                                        <th>备注</th>
                                    </tr>
                                    </thead>
                                    <tbody>
                                    {{# layui.each(d, function(index, item){ }}
                                    <tr>
                                        <td style="text-align: center;">{{ item.created_at }}</td>
                                        <td>{{ item.username }}</td>
                                        <td>{{ item.action }}</td>
                                        <td>{{ item.note }}</td>
                                    </tr>
                                    {{# }); }}
                                    </tbody>
                                </table>
                            </script>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/html" template lay-done="layui.data.sendParams(d.params)"></script>
<script>
    layui.data.sendParams = function (params) {
        layui.use(['common', 'admin', 'table', 'laytpl'], function () {
            var common = layui.common,
                admin = layui.admin,
                laytpl = layui.laytpl,
                form = layui.form,
                $ = layui.jquery,
                lay_id_prefix = 'order-order-detail',//当前页id前缀
                api_action_url = layui.setter.apiHost + '/order/order';

            //详情
            function get_detail() {
                admin.req({
                    url: api_action_url + "/detail",
                    data: {id: params.id},
                    success: function (result) {
                        if (result.code == 0) {
                            laytpl($('#detail_tpl').html()).render(result.data, function (html) {
                                $('#detail').html(html);
                            })
                            form.render();
                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    error: function () {
                        layer.msg('操作失败，请刷新页面重试！');
                    }
                });
            }

            get_detail();

            //发货信息
            function get_delivery() {
                admin.req({
                    url: api_action_url + "/get_delivery",
                    data: {id: params.id},
                    success: function (result) {
                        if (result.code == 0) {
                            laytpl($('#delivery_tpl').html()).render(result.data, function (html) {
                                $('#delivery').html(html);
                            })
                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    error: function () {
                        layer.msg('操作失败，请刷新页面重试！');
                    }
                });
            }

            get_delivery();

            //订单日志信息
            function get_log() {
                admin.req({
                    url: api_action_url + "/get_log",
                    data: {id: params.id},
                    success: function (result) {
                        if (result.code == 0) {
                            laytpl($('#log_tpl').html()).render(result.data, function (html) {
                                $('#log').html(html);
                            })
                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    error: function () {
                        layer.msg('操作失败，请刷新页面重试！');
                    }
                });
            }

            get_log();

            //监听发货提交
            form.on('submit(' + lay_id_prefix + '-delivery)', function (data) {
                form_data = data.field;
                form_action('delivery', form_data);
                return false;
            });

            //监听支付提交
            form.on('submit(' + lay_id_prefix + '-pay)', function (data) {
                layer.prompt({title: '请输入备注', formType: 2}, function (note, index) {
                    form_data = data.field;
                    form_data['note'] = note;
                    form_action('pay', form_data);
                    layer.close(index);
                });
                return false;
            });

            //监听取消提交
            form.on('submit(' + lay_id_prefix + '-cancel)', function (data) {
                layer.prompt({title: '请输入备注', formType: 2}, function (note, index) {
                    form_data = data.field;
                    form_data['note'] = note;
                    form_action('cancel', form_data);
                    layer.close(index);
                });
                return false;
            });

            function form_action(type, form_data) {
                admin.req({
                    url: api_action_url + "/" + type,
                    data: form_data,
                    success: function (result) {
                        if (result.code == 0) {
                            layer.msg('操作成功', {time: 1000}, function () {
                                get_detail();
                                get_delivery();
                                get_log();
                            })
                        } else {
                            layer.msg(result.msg);
                        }
                    },
                    error: function () {
                        layer.msg('操作失败，请刷新页面重试！');
                    }
                });
            }

            //查看物流信息
            $('#delivery').on('click', '[type="button"]', function () {
                var filter = $(this).attr('lay-filter');
                if (filter == 'order-order-detail-delivery_traces') {
                    var default_data = {company_code: $(this).attr('data-company_code'), code: $(this).attr('data-code')}
                    common.open_window('物流信息', api_action_url, 'order/order/delivery_traces', 'order-order-delivery_traces', {width: '900px', height: '500px'}, default_data)
                }
            })
        });
    }
</script>
